package com.yunpuvip.sachet.rest.controller.common;


import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.yunpuvip.sachet.biz.entity.Player;
import com.yunpuvip.sachet.biz.service.PlayerService;
import com.yunpuvip.sachet.modular.core.exception.RestException;
import com.yunpuvip.sachet.modular.core.exception.RestServiceExceptionEnum;
import com.yunpuvip.sachet.modular.util.kit.HttpKit;
import com.yunpuvip.sachet.rest.config.properties.JwtProperties;
import com.yunpuvip.sachet.rest.modular.auth.util.JwtTokenUtil;
import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * rest 通用controller
 *
 * @Author: hyf
 * @Date: 2018/7/18
 **/
public class RestBaseController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private PlayerService playerService;

    public static <T> Map<String, T> dataMap(String name, T data) {
        HashMap<String, T> hashMap = new HashMap<>();
        hashMap.put(name, data);
        return hashMap;
    }

    /**
     * 组装异常 ResponseData
     *
     * @param restServiceExceptionEnum 异常枚举类
     * @return cn.stylefeng.roses.core.reqres.response.ResponseData
     * @author hyf
     * @date 2019-03-07
     */
    protected ResponseData packErrorResponseData(RestServiceExceptionEnum restServiceExceptionEnum) {
        return ResponseData.error(restServiceExceptionEnum.getCode(), restServiceExceptionEnum.getMessage());
    }

    /**
     * 从请求头中获取 登录的玩家id
     *
     * @return java.lang.Integer
     * @author hyf
     * @date 2019-03-12
     */
    private Integer getPlayerId() {
        HttpServletRequest request = HttpKit.getRequest();
        String header = request.getHeader(jwtProperties.getHeader());
        if (StringUtils.isBlank(header)) {
            throw RestException.createRestException(RestServiceExceptionEnum.TOKEN_EXPIRED);
        }
        String authToken = header.substring(jwtProperties.getPrefix().length());
        Object playerId = null;
        try {
            playerId = jwtTokenUtil.getSubjectFromToken(authToken);
        } catch (SignatureException exception) {
            throw RestException.createRestException(RestServiceExceptionEnum.PLAYER_NOT_FOUND);
        }
        if (playerId == null || playerId.toString().equals("null")) {
            throw RestException.createRestException(RestServiceExceptionEnum.PLAYER_NOT_FOUND);
        }
        System.out.println("playerId：" + playerId);
        return Integer.valueOf(playerId.toString());
    }

    /**
     * 获取用户实体类
     *
     * @return com.yunpuvip.sachet.biz.entity.Player
     * @author hyf
     * @date 2019-03-12
     */
    protected Player getPlayer() {
        Integer playerId = this.getPlayerId();
        return playerService.getPlayer(playerId);
    }

}

